package com.jiac.coderunner.controller;

import com.jiac.coderunner.util.*;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.sql.Connection;
import java.sql.DriverManager;

@RestController
public class LoginController {

    @RequestMapping("/login")
    public OutputJson login(String address,String port,String dbName,String userName,String passWord){
        OutputJson opjson = new OutputJson();
        String url = "jdbc:mysql://"+address+":"+port+"/"+dbName+"?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true";
        try {
            Connection connection = DriverManager.getConnection(url, userName, passWord);
            if (connection!=null) {
                DBCache.ip = address;
                DBCache.port = port;
                DBCache.dbName = dbName;
                DBCache.passWord = passWord;
                DBCache.userName = userName;
            }
            opjson.setSuccess(true);
            opjson.setMsg("登录成功");
        } catch (Exception e) {
            opjson.setSuccess(false);
            opjson.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return opjson;
    }
}
